#include "logindialog.h"
#include "ui_logindialog.h"
#include <QCompleter>
#include <QDebug>
#include <QMessageBox>
#include <QSqlRecord>
#include <QSqlTableModel>


loginDialog::loginDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::loginDialog)
{
    ui->setupUi(this);
    setWindowTitle(tr("登陆"));//窗口标题
    QStringList strings;
    strings << "Lily" << "Tom" << "Jack" << "小强" << "MacBook Pro" << "Mac Pro";
    completer = new QCompleter(strings, this);
    ui->comboBox->clear();
    ui->comboBox->addItems(strings);
    ui->comboBox->setEditable(true);
    ui->comboBox->setCompleter(completer);
    qDebug()<<__FUNCTION__<<"is running !";

    ui->lineEditNumber->setEchoMode(QLineEdit::Password);//密码方式显示文本
    QDialog::setModal( true );//设置模态，让该窗口一直在最前端
    setAttribute(Qt::WA_DeleteOnClose,true);//关闭后释放内存
    ui->lineEditNumber->setFocus();//设置焦点
}

loginDialog::~loginDialog()
{
    qDebug()<<__FUNCTION__<<"is running !";
    delete ui;
}

void loginDialog::on_pushButtonLogin_clicked()
{
    qDebug()<<ui->comboBox->currentText();

//    if(ui->lineEditNumber->text() == passWord)
//    {
//        qDebug()<<"password is right!";
//        emit passWordIsRight();//密码正确传信号给Mainwindow,从而显示参数设置界面
//        this->close();
//    }
//    else
//    {
//        QMessageBox msgBox(this);
//         msgBox.setText("The password is wrong.");
//         msgBox.exec();
//    }
    QSqlTableModel model;
    model.setTable("user");
    model.select();
    int userCnt = 0;
    for(int i=0;i<model.rowCount();++i)
    {
        QSqlRecord record = model.record(i);
        QString name = record.value("name").toString();
        QString passWord = record.value("passWord").toString();
        qDebug()<<name<<passWord<<__FUNCTION__;
        if(ui->comboBox->currentText() == name){
            qDebug()<<"用户名正确!";
            if(ui->lineEditNumber->text() == passWord){
                emit passWordIsRight();//密码正确传信号给Mainwindow,从而显示参数设置界面
                this->close();
            }
            else{
                QMessageBox msgBox(this);
                msgBox.setText("The password is wrong.");
                msgBox.exec();
            }
            userCnt++;
        }
    }
    if(!userCnt)
    {
        QMessageBox msgBox(this);
        msgBox.setText("该用户不存在！");
        msgBox.exec();
    }
}
void loginDialog::on_pushButtonEnter_clicked()
{
//    QString str = ui->lineEditNumber->text();
//    ui->lineEditNumber->clear();
//    this->close();
}

void loginDialog::on_pushButton1_clicked()
{
    ui->lineEditNumber->setText(ui->lineEditNumber->text()+ui->pushButton1->text());
}

void loginDialog::on_pushButton2_clicked()
{
    ui->lineEditNumber->setText(ui->lineEditNumber->text()+ui->pushButton2->text());
}

void loginDialog::on_pushButton3_clicked()
{
    ui->lineEditNumber->setText(ui->lineEditNumber->text()+ui->pushButton3->text());
}


void loginDialog::on_pushButton4_clicked()
{
    ui->lineEditNumber->setText(ui->lineEditNumber->text()+ui->pushButton4->text());
}

void loginDialog::on_pushButton5_clicked()
{
    ui->lineEditNumber->setText(ui->lineEditNumber->text()+ui->pushButton5->text());
}

void loginDialog::on_pushButton6_clicked()
{
    ui->lineEditNumber->setText(ui->lineEditNumber->text()+ui->pushButton6->text());
}

void loginDialog::on_pushButton7_clicked()
{
    ui->lineEditNumber->setText(ui->lineEditNumber->text()+ui->pushButton7->text());
}

void loginDialog::on_pushButton8_clicked()
{
    ui->lineEditNumber->setText(ui->lineEditNumber->text()+ui->pushButton8->text());
}

void loginDialog::on_pushButton9_clicked()
{
    ui->lineEditNumber->setText(ui->lineEditNumber->text()+ui->pushButton9->text());
}

void loginDialog::on_pushButton0_clicked()
{
    ui->lineEditNumber->setText(ui->lineEditNumber->text()+ui->pushButton0->text());
}

void loginDialog::on_pushButtonBack_clicked()
{
    QString text = ui->lineEditNumber->text();
    text.chop(1);//Removes n characters from the end of the string.
//    if (text.isEmpty()) {
//        text = "0";
//    }
    ui->lineEditNumber->setText(text);
}
